﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using KeaoBase;
using System.IO;
using System.Collections;
using System.Xml;
using System.Reflection;
using GoldPrinter;
using System.Web;
using System.Collections.Specialized;
using ServiceStack.Redis;
using System.Security.Cryptography;

namespace KeaoReportService
{
    public partial class Form_Report : Form
    {
        public Form_Report()
        {
            InitializeComponent();
        }

        private HttpListener l = new HttpListener();
        private Hashtable m_DBList = new Hashtable();//数据库列表
        private Hashtable m_PathList = new Hashtable();//图片存储地址

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                l.Prefixes.Add(string.Format("http://{0}:{1}/", KeaoUtils.ConfigGetNodeAttr("端口", "IP"), KeaoUtils.ConfigGetNodeAttr("端口", "Port")));
                l.Start();
                l.BeginGetContext(new AsyncCallback(CallBack), l);

                InitConfig();

            }
            catch (Exception err)
            {
                KeaoUtils.LogTxt(string.Format("初始化报表服务失败。{0}", err.Message));
            }
        }

        private void InitConfig()
        {
            try
            {
                m_DBList.Clear();
                m_PathList.Clear();
                lst_client.Items.Clear();
                XmlNodeList nodeList = KeaoUtils.ConfigGetNodeListByTag("客户端");
                foreach (XmlNode node in nodeList)
                {
                    BaseDataBase newDB = Assembly.LoadFrom("KeaoDataBase.dll").CreateInstance(string.Format("KeaoDataBase.{0}", node.Attributes["数据库类型"].Value)) as BaseDataBase;
                    if (!newDB.InitDataBase(node.Attributes["数据库"].Value))
                    {
                        KeaoUtils.LogTxt(string.Format("数据库连接失败。{0}", node.Attributes["客户编号"].Value));
                    }

                    m_DBList.Add(node.Attributes["客户编号"].Value, newDB);
                    m_PathList.Add(node.Attributes["客户编号"].Value, node.Attributes["报表地址"].Value);

                    lst_client.Items.Add(string.Format("{0}报表服务启动。", node.InnerText));
                }
            }
            catch (Exception err)
            {
                KeaoUtils.LogTxt(string.Format("初始化报表服务失败。{0}", err.Message));
            }
        }

        private void CallBack(IAsyncResult ar)
        {
            l = ar.AsyncState as HttpListener;
            HttpListenerContext currentC = l.EndGetContext(ar);
            l.BeginGetContext(new AsyncCallback(CallBack), l);

            //string uid = currentC.Request.QueryString["uid"];
            //string kid = currentC.Request.QueryString["kid"];
            //if (!Auth.UserLogined(uid, kid))
            //{
            //    byte[] bs = Encoding.Default.GetBytes("<center>用户信息验证失败。</center>");
            //    currentC.Response.OutputStream.Write(bs, 0, bs.Length);
            //    currentC.Response.Close();
            //    return;
            //}

            string requestType =currentC.Request.QueryString["requestType"];
            string keHuBianHao = currentC.Request.QueryString["keHuBianHao"];

            if (string.IsNullOrEmpty(requestType) || string.IsNullOrEmpty(keHuBianHao))
            {
                byte[] bs = Encoding.Default.GetBytes("<center>没有可导出的数据。</center>");
                currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                currentC.Response.Close();
                return;
            }

            MD5 md5 = new MD5CryptoServiceProvider();
            string fileNameResult = BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(currentC.Request.RawUrl))).Replace("-", "");
            string hasFile = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], fileNameResult);
            if (File.Exists(hasFile))
            {
                string strHeader = "attachment;filename=" + string.Format("{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmss"));//把文件头输出，此文件头激活文件下载框  
                currentC.Response.AppendHeader("Content-Disposition", strHeader);//http报头文件   
                currentC.Response.ContentType = "application/ms-excel";
                byte[] bs = File.ReadAllBytes(hasFile);
                currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                currentC.Response.Close();
                return;
            }

            try
            {

                BaseDataBase m_DB = m_DBList[keHuBianHao] as BaseDataBase;
                if (m_DB == null)
                {
                    return;
                }

                if (requestType == "1")
                {
                    //明细
                    NameValueCollection queryString = HttpUtility.ParseQueryString(currentC.Request.RawUrl, Encoding.UTF8);
                    string zhanDian = string.Format("{0}", queryString["zhandian"]).Replace("_", "#");
                    string chengZhongLeiXing = string.Format("{0}", queryString["chengzhongleixing"]);
                    string kaiShiShiJian = string.Format("{0}", queryString["kaishishijian"]);
                    string jieShuShiJian = string.Format("{0}", queryString["jieshushijian"]);
                    string danWeiMingCheng = string.Format("{0}", queryString["danweimingcheng"]);
                    string wuLiaoMingCheng = string.Format("{0}", queryString["wuliaomingcheng"]);
                    string xingHao = string.Format("{0}", queryString["xinghao"]);
                    string chePaiHao = string.Format("{0}", queryString["chepaihao"]);
                    string currentYongHu = string.Format("{0}", queryString["yonghu"]);

                    Dictionary<string, string> orderByCondition = new Dictionary<string, string>();
                    string createCondition = m_DB.CreateAndCondition("chengzhongleixing", DataValueCommonType.字符串, chengZhongLeiXing, "", SearchConditionType.等于);
                    if (zhanDian != "—全部—")
                    {
                        createCondition += m_DB.CreateAndCondition("zhandian", DataValueCommonType.字符串, zhanDian, "", SearchConditionType.等于);
                    }
                    if (!string.IsNullOrEmpty(kaiShiShiJian) && !string.IsNullOrEmpty(jieShuShiJian))
                    {
                        if (chengZhongLeiXing == "入库物料")
                        {
                            orderByCondition.Add("pizhongshijian", "desc");
                            createCondition += m_DB.CreateAndCondition("pizhongshijian", DataValueCommonType.日期时间, kaiShiShiJian, jieShuShiJian, SearchConditionType.between);
                        }
                        else
                        {
                            orderByCondition.Add("maozhongshijian", "desc");
                            createCondition += m_DB.CreateAndCondition("maozhongshijian", DataValueCommonType.日期时间, kaiShiShiJian, jieShuShiJian, SearchConditionType.between);
                        }
                    }
                    if (!string.IsNullOrEmpty(danWeiMingCheng) && danWeiMingCheng != "—全部—")
                    {
                        createCondition += m_DB.CreateAndCondition("danweimingcheng", DataValueCommonType.字符串, danWeiMingCheng, "", SearchConditionType.等于);
                    }
                    if (!string.IsNullOrEmpty(wuLiaoMingCheng) && wuLiaoMingCheng != "—全部—")
                    {
                        createCondition += m_DB.CreateAndCondition("wuliaomingcheng", DataValueCommonType.字符串, wuLiaoMingCheng, "", SearchConditionType.等于);
                    }
                    if (!string.IsNullOrEmpty(xingHao) && xingHao != "—全部—")
                    {
                        createCondition += m_DB.CreateAndCondition("xinghao", DataValueCommonType.字符串, xingHao, "", SearchConditionType.等于);
                    }
                    if (!string.IsNullOrEmpty(chePaiHao))
                    {
                        createCondition += m_DB.CreateAndCondition("chepaihao", DataValueCommonType.字符串, chePaiHao, "", SearchConditionType.like);
                    }

                    DataTable dtMingXi = m_DB.GetDataTableByCondition("tbl_chengzhong", createCondition, orderByCondition);
                    DataTable dtCount = m_DB.GetDataTableByCondition("tbl_chengzhong", "count(id) as cheshu,sum(kouzhong) as kouzhong,sum(jingzhong) as jingzhong,sum(fahuozhongliang) as fahuozhongliang,sum(jisuanfangliang) as jisuanfangliang", null, createCondition, null, null);
                    if (dtMingXi.Rows.Count > 0)
                    {
                        DataRow newRow = dtMingXi.NewRow();
                        newRow["xinghao"] = string.Format("合计：{0}车", dtCount.Rows[0]["cheshu"]);
                        newRow["kouzhong"] = dtCount.Rows[0]["kouzhong"];
                        newRow["jingzhong"] = dtCount.Rows[0]["jingzhong"];
                        newRow["fahuozhongliang"] = dtCount.Rows[0]["fahuozhongliang"];
                        newRow["jisuanfangliang"] = dtCount.Rows[0]["jisuanfangliang"];
                        dtMingXi.Rows.Add(newRow);

                        if (chengZhongLeiXing == "入库物料")
                        {
                            ExcelAccess excelAccess = new ExcelAccess();
                            try
                            {
                                excelAccess.Open(string.Format("{0}Template\\{1}", m_PathList[keHuBianHao], "物资入库明细报表.xls"));
                                excelAccess.SetCellText(2, "A", string.Format("报表时间：{0}——{1}", kaiShiShiJian, jieShuShiJian));
                                for (int i = 0; i < dtMingXi.Rows.Count; i++)
                                {
                                    excelAccess.SetCellText(i + 4, "A", string.Format("{0}", dtMingXi.Rows[i]["danweimingcheng"]));
                                    excelAccess.SetCellText(i + 4, "B", string.Format("{0}", dtMingXi.Rows[i]["wuliaomingcheng"]));
                                    excelAccess.SetCellText(i + 4, "C", string.Format("{0}", dtMingXi.Rows[i]["xinghao"]));
                                    excelAccess.SetCellText(i + 4, "D", string.Format("{0}", dtMingXi.Rows[i]["pizhongshijian"]));
                                    excelAccess.SetCellText(i + 4, "E", string.Format("{0}", dtMingXi.Rows[i]["jingzhong"]));
                                    excelAccess.SetCellText(i + 4, "F", string.Format("{0}", dtMingXi.Rows[i]["kouzhong"]));
                                    excelAccess.SetCellText(i + 4, "G", string.Format("{0}", dtMingXi.Rows[i]["fahuozhongliang"]));
                                }

                                excelAccess.MergeCells(4 + dtMingXi.Rows.Count, "A", 4 + dtMingXi.Rows.Count, "G", true);
                                excelAccess.SetCellText(4 + dtMingXi.Rows.Count, "A", string.Format("制表人：{0}    制表时间：{1}", currentYongHu, DateTime.Now.ToString("yyyy-MM-dd HH:mm")));

                                Excel.Range r = excelAccess.GetRange(4, "A", dtMingXi.Rows.Count + 3, "G");
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlLeft);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlRight);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlBottom);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideHorizontal);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideVertical);

                                string fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], fileNameResult);
                                if (jieShuShiJian .Contains( DateTime.Today.ToString("yyyy-MM-dd")))
                                {
                                    fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], Guid.NewGuid().ToString());
                                }
                                excelAccess.SaveAs(fileName, true);

                                string strHeader = "attachment;filename=" + string.Format("{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmss"));//把文件头输出，此文件头激活文件下载框  
                                currentC.Response.AppendHeader("Content-Disposition", strHeader);//http报头文件   
                                currentC.Response.ContentType = "application/ms-excel";
                                byte[] bs = File.ReadAllBytes(fileName);
                                currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                                currentC.Response.Close();

                            }
                            catch (Exception err)
                            {
                                KeaoUtils.LogTxt(string.Format("入库明细报表导出失败。{0}", err.Message));
                            }
                            finally
                            {
                                excelAccess.Close();
                            }
                        }
                        else
                        {
                            ExcelAccess excelAccess = new ExcelAccess();
                            try
                            {
                                excelAccess.Open(string.Format("{0}Template\\{1}", m_PathList[keHuBianHao], "物资出库明细报表.xls"));
                                excelAccess.SetCellText(2, "A", string.Format("报表时间：{0}——{1}", kaiShiShiJian, jieShuShiJian));
                                for (int i = 0; i < dtMingXi.Rows.Count; i++)
                                {
                                    excelAccess.SetCellText(i + 4, "A", string.Format("{0}", dtMingXi.Rows[i]["danweimingcheng"]));
                                    excelAccess.SetCellText(i + 4, "B", string.Format("{0}", dtMingXi.Rows[i]["wuliaomingcheng"]));
                                    excelAccess.SetCellText(i + 4, "C", string.Format("{0}", dtMingXi.Rows[i]["xinghao"]));
                                    excelAccess.SetCellText(i + 4, "D", string.Format("{0}", dtMingXi.Rows[i]["pizhongshijian"]));
                                    excelAccess.SetCellText(i + 4, "E", string.Format("{0}", dtMingXi.Rows[i]["jingzhong"]));
                                    excelAccess.SetCellText(i + 4, "F", string.Format("{0}", dtMingXi.Rows[i]["kouzhong"]));
                                    excelAccess.SetCellText(i + 4, "G", string.Format("{0}", dtMingXi.Rows[i]["quxiangbiaoduan"]));
                                }

                                excelAccess.MergeCells(4 + dtMingXi.Rows.Count, "A", 4 + dtMingXi.Rows.Count, "G", true);
                                excelAccess.SetCellText(4 + dtMingXi.Rows.Count, "A", string.Format("制表人：{0}    制表时间：{1}", currentYongHu, DateTime.Now.ToString("yyyy-MM-dd HH:mm")));

                                Excel.Range r = excelAccess.GetRange(4, "A", dtMingXi.Rows.Count + 3, "G");
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlLeft);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlRight);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlBottom);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideHorizontal);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideVertical);
                                
                                string fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], fileNameResult);
                                if (jieShuShiJian .Contains( DateTime.Today.ToString("yyyy-MM-dd")))
                                {
                                    fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], Guid.NewGuid().ToString());
                                }
                                excelAccess.SaveAs(fileName, true);

                                string strHeader = "attachment;filename=" + string.Format("{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmss"));//把文件头输出，此文件头激活文件下载框  
                                currentC.Response.AppendHeader("Content-Disposition", strHeader);//http报头文件   
                                currentC.Response.ContentType = "application/ms-excel";
                                byte[] bs = File.ReadAllBytes(fileName);
                                currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                                currentC.Response.Close();

                            }
                            catch (Exception err)
                            {
                                KeaoUtils.LogTxt(string.Format("出库明细报表导出失败。{0}", err.Message));
                            }
                            finally
                            {
                                excelAccess.Close();
                            }
                        }

                    }
                    else
                    {
                        byte[] bs = Encoding.Default.GetBytes("<center>没有可导出的数据。</center>");
                        currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                        currentC.Response.Close();
                        return;
                    }
                }

                if (requestType == "2")
                {
                    //日期汇总
                    NameValueCollection queryString = HttpUtility.ParseQueryString(currentC.Request.RawUrl, Encoding.UTF8);
                    string zhanDian = string.Format("{0}", queryString["zhandian"]).Replace("_", "#");
                    string chengZhongLeiXing = string.Format("{0}", queryString["chengzhongleixing"]);
                    string kaiShiShiJian = string.Format("{0}", queryString["kaishishijian"]);
                    string jieShuShiJian = string.Format("{0}", queryString["jieshushijian"]);
                    string wuLiaoMingCheng = string.Format("{0}", queryString["wuliaomingcheng"]);
                    string xingHao = string.Format("{0}", queryString["xinghao"]);
                    string currentYongHu = string.Format("{0}", queryString["yonghu"]);

                    string createCondition = m_DB.CreateAndCondition(chengZhongLeiXing == "入库物料" ? "pizhongshijian" : "maozhongshijian", DataValueCommonType.日期时间, string.Format("{0} 00:00:00", kaiShiShiJian), string.Format("{0} 23:59:59", jieShuShiJian), SearchConditionType.between);
                    createCondition += m_DB.CreateAndCondition("chengzhongleixing", DataValueCommonType.字符串, chengZhongLeiXing, "", SearchConditionType.等于);
                    if (zhanDian != "—全部—")
                    {
                        createCondition += m_DB.CreateAndCondition("zhandian", DataValueCommonType.字符串, zhanDian, "", SearchConditionType.等于);
                    }
                    if (!string.IsNullOrEmpty(wuLiaoMingCheng) && wuLiaoMingCheng != "—全部—")
                    {
                        createCondition += m_DB.CreateAndCondition("wuliaomingcheng", DataValueCommonType.字符串, wuLiaoMingCheng, "", SearchConditionType.等于);
                    }
                    if (!string.IsNullOrEmpty(xingHao) && xingHao != "—全部—")
                    {
                        createCondition += m_DB.CreateAndCondition("xinghao", DataValueCommonType.字符串, xingHao, "", SearchConditionType.等于);
                    }
                    string tableCols = string.Empty;
                    string groupCols = string.Empty;
                    Dictionary<string, string> orderBy = new Dictionary<string, string>();
                    orderBy.Add("wuliaomingcheng", "asc");
                    orderBy.Add("xinghao", "asc");

                    if (chengZhongLeiXing == "入库物料")
                    {
                        tableCols = string.Format("{0} as riqi,wuliaomingcheng,xinghao,count(id) as cheshu,sum(maozhong) as maozhong,sum(pizhong) as pizhong,sum(kouzhong) as kouzhong,sum(jingzhong) as jingzhong,sum(fahuozhongliang) as fahuozhongliang,sum(jisuanfangliang) as jisuanfangliang", m_DB.DateFormatFun("pizhongshijian"));
                        groupCols = string.Format("{0},wuliaomingcheng,xinghao", m_DB.DateFormatFun("pizhongshijian"));

                    }
                    else
                    {
                        tableCols = string.Format("{0} as riqi,wuliaomingcheng,xinghao,count(id) as cheshu,sum(maozhong) as maozhong,sum(pizhong) as pizhong,sum(kouzhong) as kouzhong,sum(jingzhong) as jingzhong,sum(fahuozhongliang) as fahuozhongliang,sum(jisuanfangliang) as jisuanfangliang", m_DB.DateFormatFun("maozhongshijian"));
                        groupCols = string.Format("{0},wuliaomingcheng,xinghao", m_DB.DateFormatFun("maozhongshijian"));
                    }

                    DataTable Dt = m_DB.GetDataTableByCondition("tbl_chengzhong", tableCols, null, createCondition, groupCols, orderBy);
                    tableCols = "wuliaomingcheng,xinghao,count(id) as cheshu,sum(maozhong) as maozhong,sum(pizhong) as pizhong,sum(kouzhong) as kouzhong,sum(jingzhong) as jingzhong,sum(fahuozhongliang) as fahuozhongliang,sum(jisuanfangliang) as jisuanfangliang";
                    groupCols = "wuliaomingcheng,xinghao";
                    DataTable DtCount = m_DB.GetDataTableByCondition("tbl_chengzhong", tableCols, null, createCondition, groupCols, orderBy);
                    tableCols = "count(id) as cheshu,sum(maozhong) as maozhong,sum(pizhong) as pizhong,sum(kouzhong) as kouzhong,sum(jingzhong) as jingzhong,sum(fahuozhongliang) as fahuozhongliang,sum(jisuanfangliang) as jisuanfangliang";
                    DataTable DtCountAll = m_DB.GetDataTableByCondition("tbl_chengzhong", tableCols, null, createCondition, null, null);
                    if (Dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < Dt.Rows.Count; i++)
                        {
                            if (i == Dt.Rows.Count - 1)
                            {
                                DataRow newRow = Dt.NewRow();
                                newRow["xinghao"] = "合计：";
                                newRow["cheshu"] = DtCount.Rows[0]["cheshu"];
                                newRow["maozhong"] = DtCount.Rows[0]["maozhong"];
                                newRow["pizhong"] = DtCount.Rows[0]["pizhong"];
                                newRow["kouzhong"] = DtCount.Rows[0]["kouzhong"];
                                newRow["jingzhong"] = DtCount.Rows[0]["jingzhong"];
                                newRow["fahuozhongliang"] = DtCount.Rows[0]["fahuozhongliang"];
                                newRow["jisuanfangliang"] = DtCount.Rows[0]["jisuanfangliang"];
                                Dt.Rows.InsertAt(newRow, i + 1);
                                DtCount.Rows.RemoveAt(0);
                                i++;
                                break;
                            }
                            if (!Dt.Rows[i]["wuliaomingcheng"].Equals(Dt.Rows[i + 1]["wuliaomingcheng"]) || !Dt.Rows[i]["xinghao"].Equals(Dt.Rows[i + 1]["xinghao"]))
                            {
                                DataRow newRow = Dt.NewRow();
                                newRow["xinghao"] = "合计：";
                                newRow["cheshu"] = DtCount.Rows[0]["cheshu"];
                                newRow["maozhong"] = DtCount.Rows[0]["maozhong"];
                                newRow["pizhong"] = DtCount.Rows[0]["pizhong"];
                                newRow["kouzhong"] = DtCount.Rows[0]["kouzhong"];
                                newRow["jingzhong"] = DtCount.Rows[0]["jingzhong"];
                                newRow["fahuozhongliang"] = DtCount.Rows[0]["fahuozhongliang"];
                                newRow["jisuanfangliang"] = DtCount.Rows[0]["jisuanfangliang"];
                                Dt.Rows.InsertAt(newRow, i + 1);
                                DtCount.Rows.RemoveAt(0);
                                i++;
                            }
                        }

                        DataRow countAllRow = Dt.NewRow();
                        countAllRow["xinghao"] = "总计：";
                        countAllRow["cheshu"] = DtCountAll.Rows[0]["cheshu"];
                        countAllRow["maozhong"] = DtCountAll.Rows[0]["maozhong"];
                        countAllRow["pizhong"] = DtCountAll.Rows[0]["pizhong"];
                        countAllRow["kouzhong"] = DtCountAll.Rows[0]["kouzhong"];
                        countAllRow["jingzhong"] = DtCountAll.Rows[0]["jingzhong"];
                        countAllRow["fahuozhongliang"] = DtCountAll.Rows[0]["fahuozhongliang"];
                        countAllRow["jisuanfangliang"] = DtCountAll.Rows[0]["jisuanfangliang"];
                        Dt.Rows.Add(countAllRow);

                        if (chengZhongLeiXing == "入库物料")
                        {
                            ExcelAccess excelAccess = new ExcelAccess();
                            try
                            {
                                excelAccess.Open(string.Format("{0}Template\\{1}", m_PathList[keHuBianHao], "入库物资日期汇总报表.xls"));
                                excelAccess.SetCellText(2, "A", string.Format("报表时间：{0}——{1}", kaiShiShiJian, jieShuShiJian));
                                for (int i = 0; i < Dt.Rows.Count; i++)
                                {
                                    excelAccess.SetCellText(i + 4, "A", string.Format("{0}", Dt.Rows[i]["riqi"]));
                                    excelAccess.SetCellText(i + 4, "B", string.Format("{0}", Dt.Rows[i]["wuliaomingcheng"]));
                                    excelAccess.SetCellText(i + 4, "C", string.Format("{0}", Dt.Rows[i]["xinghao"]));
                                    excelAccess.SetCellText(i + 4, "D", string.Format("{0}", Dt.Rows[i]["cheshu"]));
                                    excelAccess.SetCellText(i + 4, "E", string.Format("{0}", Dt.Rows[i]["jingzhong"]));
                                    excelAccess.SetCellText(i + 4, "F", string.Format("{0}", Dt.Rows[i]["kouzhong"]));
                                    excelAccess.SetCellText(i + 4, "G", string.Format("{0}", Dt.Rows[i]["fahuozhongliang"]));
                                }

                                excelAccess.MergeCells(4 + Dt.Rows.Count, "A", 4 + Dt.Rows.Count, "G", true);
                                excelAccess.SetCellText(4 + Dt.Rows.Count, "A", string.Format("制表人：{0}    制表时间：{1}", currentYongHu, DateTime.Now.ToString("yyyy-MM-dd HH:mm")));

                                Excel.Range r = excelAccess.GetRange(4, "A", Dt.Rows.Count + 3, "G");
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlLeft);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlRight);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlBottom);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideHorizontal);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideVertical);

                                string fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], fileNameResult);
                                if (jieShuShiJian .Contains( DateTime.Today.ToString("yyyy-MM-dd")))
                                {
                                    fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], Guid.NewGuid().ToString());
                                }
                                excelAccess.SaveAs(fileName, true);

                                string strHeader = "attachment;filename=" + string.Format("{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmss"));//把文件头输出，此文件头激活文件下载框  
                                currentC.Response.AppendHeader("Content-Disposition", strHeader);//http报头文件   
                                currentC.Response.ContentType = "application/ms-excel";
                                byte[] bs = File.ReadAllBytes(fileName);
                                currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                                currentC.Response.Close();

                            }
                            catch (Exception err)
                            {
                                KeaoUtils.LogTxt(string.Format("入库日期汇总报表导出失败。{0}", err.Message));
                            }
                            finally
                            {
                                excelAccess.Close();
                            }
                        }
                        else
                        {
                            ExcelAccess excelAccess = new ExcelAccess();
                            try
                            {
                                excelAccess.Open(string.Format("{0}Template\\{1}", m_PathList[keHuBianHao], "出库物资日期汇总报表.xls"));
                                excelAccess.SetCellText(2, "A", string.Format("报表时间：{0}——{1}", kaiShiShiJian, jieShuShiJian));
                                for (int i = 0; i < Dt.Rows.Count; i++)
                                {
                                    excelAccess.SetCellText(i + 4, "A", string.Format("{0}", Dt.Rows[i]["riqi"]));
                                    excelAccess.SetCellText(i + 4, "B", string.Format("{0}", Dt.Rows[i]["wuliaomingcheng"]));
                                    excelAccess.SetCellText(i + 4, "C", string.Format("{0}", Dt.Rows[i]["xinghao"]));
                                    excelAccess.SetCellText(i + 4, "D", string.Format("{0}", Dt.Rows[i]["cheshu"]));
                                    excelAccess.SetCellText(i + 4, "E", string.Format("{0}", Dt.Rows[i]["jingzhong"]));
                                    excelAccess.SetCellText(i + 4, "F", string.Format("{0}", Dt.Rows[i]["kouzhong"]));
                                }

                                excelAccess.MergeCells(4 + Dt.Rows.Count, "A", 4 + Dt.Rows.Count, "F", true);
                                excelAccess.SetCellText(4 + Dt.Rows.Count, "A", string.Format("制表人：{0}    制表时间：{1}", currentYongHu, DateTime.Now.ToString("yyyy-MM-dd HH:mm")));

                                Excel.Range r = excelAccess.GetRange(4, "A", Dt.Rows.Count + 3, "F");
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlLeft);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlRight);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlBottom);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideHorizontal);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideVertical);

                                string fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], fileNameResult);
                                if (jieShuShiJian .Contains( DateTime.Today.ToString("yyyy-MM-dd")))
                                {
                                    fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], Guid.NewGuid().ToString());
                                }
                                excelAccess.SaveAs(fileName, true);

                                string strHeader = "attachment;filename=" + string.Format("{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmss"));//把文件头输出，此文件头激活文件下载框  
                                currentC.Response.AppendHeader("Content-Disposition", strHeader);//http报头文件   
                                currentC.Response.ContentType = "application/ms-excel";
                                byte[] bs = File.ReadAllBytes(fileName);
                                currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                                currentC.Response.Close();

                            }
                            catch (Exception err)
                            {
                                KeaoUtils.LogTxt(string.Format("出库日期汇总报表导出失败。{0}", err.Message));
                            }
                            finally
                            {
                                excelAccess.Close();
                            }
                        }
                    }
                    else
                    {
                        byte[] bs = Encoding.Default.GetBytes("<center>没有可导出的数据。</center>");
                        currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                        currentC.Response.Close();
                        return;
                    }
                }

                if (requestType == "3")
                {
                    //单位汇总
                    NameValueCollection queryString = HttpUtility.ParseQueryString(currentC.Request.RawUrl, Encoding.UTF8);
                    string zhanDian = string.Format("{0}", queryString["zhandian"]).Replace("_", "#");
                    string chengZhongLeiXing = string.Format("{0}", queryString["chengzhongleixing"]);
                    string kaiShiShiJian = string.Format("{0}", queryString["kaishishijian"]);
                    string jieShuShiJian = string.Format("{0}", queryString["jieshushijian"]);
                    string wuLiaoMingCheng = string.Format("{0}", queryString["wuliaomingcheng"]);
                    string xingHao = string.Format("{0}", queryString["xinghao"]);
                    string currentYongHu = string.Format("{0}", queryString["yonghu"]);

                    string createCondition = m_DB.CreateAndCondition(chengZhongLeiXing == "入库物料" ? "pizhongshijian" : "maozhongshijian", DataValueCommonType.日期时间, string.Format("{0} 00:00:00", kaiShiShiJian), string.Format("{0} 23:59:59", jieShuShiJian), SearchConditionType.between);
                    createCondition += m_DB.CreateAndCondition("chengzhongleixing", DataValueCommonType.字符串, chengZhongLeiXing, "", SearchConditionType.等于);
                    if (!string.IsNullOrEmpty(wuLiaoMingCheng) && wuLiaoMingCheng != "—全部—")
                    {
                        createCondition += m_DB.CreateAndCondition("wuliaomingcheng", DataValueCommonType.字符串, wuLiaoMingCheng, "", SearchConditionType.等于);
                    }
                    if (!string.IsNullOrEmpty(xingHao) && xingHao != "—全部—")
                    {
                        createCondition += m_DB.CreateAndCondition("xinghao", DataValueCommonType.字符串, xingHao, "", SearchConditionType.等于);
                    }
                    string tableCols = string.Empty;
                    string groupCols = string.Empty;
                    Dictionary<string, string> orderBy = new Dictionary<string, string>();
                    orderBy.Add("wuliaomingcheng", "asc");
                    orderBy.Add("xinghao", "asc");
                    tableCols = "danweimingcheng,wuliaomingcheng,xinghao,count(id) as cheshu,sum(maozhong) as maozhong,sum(pizhong) as pizhong,sum(kouzhong) as kouzhong,sum(jingzhong) as jingzhong,sum(fahuozhongliang) as fahuozhongliang,sum(jisuanfangliang) as jisuanfangliang";
                    groupCols = "danweimingcheng,wuliaomingcheng,xinghao";
                    DataTable Dt = m_DB.GetDataTableByCondition("tbl_chengzhong", tableCols, null, createCondition, groupCols, orderBy);
                    tableCols = "wuliaomingcheng,xinghao,count(id) as cheshu,sum(maozhong) as maozhong,sum(pizhong) as pizhong,sum(kouzhong) as kouzhong,sum(jingzhong) as jingzhong,sum(fahuozhongliang) as fahuozhongliang,sum(jisuanfangliang) as jisuanfangliang";
                    groupCols = "wuliaomingcheng,xinghao";
                    DataTable DtCount = m_DB.GetDataTableByCondition("tbl_chengzhong", tableCols, null, createCondition, groupCols, orderBy);
                    tableCols = "count(id) as cheshu,sum(maozhong) as maozhong,sum(pizhong) as pizhong,sum(kouzhong) as kouzhong,sum(jingzhong) as jingzhong,sum(fahuozhongliang) as fahuozhongliang,sum(jisuanfangliang) as jisuanfangliang";
                    DataTable DtCountAll = m_DB.GetDataTableByCondition("tbl_chengzhong", tableCols, null, createCondition, null, null);
                    if (Dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < Dt.Rows.Count; i++)
                        {
                            if (i == Dt.Rows.Count - 1)
                            {
                                DataRow newRow = Dt.NewRow();
                                newRow["xinghao"] = "合计：";
                                newRow["cheshu"] = DtCount.Rows[0]["cheshu"];
                                newRow["maozhong"] = DtCount.Rows[0]["maozhong"];
                                newRow["pizhong"] = DtCount.Rows[0]["pizhong"];
                                newRow["kouzhong"] = DtCount.Rows[0]["kouzhong"];
                                newRow["jingzhong"] = DtCount.Rows[0]["jingzhong"];
                                newRow["fahuozhongliang"] = DtCount.Rows[0]["fahuozhongliang"];
                                newRow["jisuanfangliang"] = DtCount.Rows[0]["jisuanfangliang"];
                                Dt.Rows.InsertAt(newRow, i + 1);
                                DtCount.Rows.RemoveAt(0);
                                i++;
                                break;
                            }
                            if (!Dt.Rows[i]["wuliaomingcheng"].Equals(Dt.Rows[i + 1]["wuliaomingcheng"]) || !Dt.Rows[i]["xinghao"].Equals(Dt.Rows[i + 1]["xinghao"]))
                            {
                                DataRow newRow = Dt.NewRow();
                                newRow["xinghao"] = "合计：";
                                newRow["cheshu"] = DtCount.Rows[0]["cheshu"];
                                newRow["maozhong"] = DtCount.Rows[0]["maozhong"];
                                newRow["pizhong"] = DtCount.Rows[0]["pizhong"];
                                newRow["kouzhong"] = DtCount.Rows[0]["kouzhong"];
                                newRow["jingzhong"] = DtCount.Rows[0]["jingzhong"];
                                newRow["fahuozhongliang"] = DtCount.Rows[0]["fahuozhongliang"];
                                newRow["jisuanfangliang"] = DtCount.Rows[0]["jisuanfangliang"];
                                Dt.Rows.InsertAt(newRow, i + 1);
                                DtCount.Rows.RemoveAt(0);
                                i++;
                            }
                        }

                        DataRow countAllRow = Dt.NewRow();
                        countAllRow["xinghao"] = "总计：";
                        countAllRow["cheshu"] = DtCountAll.Rows[0]["cheshu"];
                        countAllRow["maozhong"] = DtCountAll.Rows[0]["maozhong"];
                        countAllRow["pizhong"] = DtCountAll.Rows[0]["pizhong"];
                        countAllRow["kouzhong"] = DtCountAll.Rows[0]["kouzhong"];
                        countAllRow["jingzhong"] = DtCountAll.Rows[0]["jingzhong"];
                        countAllRow["fahuozhongliang"] = DtCountAll.Rows[0]["fahuozhongliang"];
                        countAllRow["jisuanfangliang"] = DtCountAll.Rows[0]["jisuanfangliang"];
                        Dt.Rows.Add(countAllRow);
                        if (chengZhongLeiXing == "入库物料")
                        {
                            ExcelAccess excelAccess = new ExcelAccess();
                            try
                            {
                                excelAccess.Open(string.Format("{0}Template\\{1}", m_PathList[keHuBianHao], "入库物资单位汇总报表.xls"));
                                excelAccess.SetCellText(2, "A", string.Format("报表时间：{0}——{1}", kaiShiShiJian, jieShuShiJian));
                                for (int i = 0; i < Dt.Rows.Count; i++)
                                {
                                    excelAccess.SetCellText(i + 4, "A", string.Format("{0}", Dt.Rows[i]["danweimingcheng"]));
                                    excelAccess.SetCellText(i + 4, "B", string.Format("{0}", Dt.Rows[i]["wuliaomingcheng"]));
                                    excelAccess.SetCellText(i + 4, "C", string.Format("{0}", Dt.Rows[i]["xinghao"]));
                                    excelAccess.SetCellText(i + 4, "D", string.Format("{0}", Dt.Rows[i]["cheshu"]));
                                    excelAccess.SetCellText(i + 4, "E", string.Format("{0}", Dt.Rows[i]["jingzhong"]));
                                    excelAccess.SetCellText(i + 4, "F", string.Format("{0}", Dt.Rows[i]["kouzhong"]));
                                    excelAccess.SetCellText(i + 4, "G", string.Format("{0}", Dt.Rows[i]["fahuozhongliang"]));
                                }

                                excelAccess.MergeCells(4 + Dt.Rows.Count, "A", 4 + Dt.Rows.Count, "G", true);
                                excelAccess.SetCellText(4 + Dt.Rows.Count, "A", string.Format("制表人：{0}    制表时间：{1}", currentYongHu, DateTime.Now.ToString("yyyy-MM-dd HH:mm")));

                                Excel.Range r = excelAccess.GetRange(4, "A", Dt.Rows.Count + 3, "G");
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlLeft);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlRight);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlBottom);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideHorizontal);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideVertical);

                                string fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], fileNameResult);
                                if (jieShuShiJian .Contains( DateTime.Today.ToString("yyyy-MM-dd")))
                                {
                                    fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], Guid.NewGuid().ToString());
                                }
                                excelAccess.SaveAs(fileName, true);

                                string strHeader = "attachment;filename=" + string.Format("{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmss"));//把文件头输出，此文件头激活文件下载框  
                                currentC.Response.AppendHeader("Content-Disposition", strHeader);//http报头文件   
                                currentC.Response.ContentType = "application/ms-excel";
                                byte[] bs = File.ReadAllBytes(fileName);
                                currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                                currentC.Response.Close();

                            }
                            catch (Exception err)
                            {
                                KeaoUtils.LogTxt(string.Format("入库单位汇总报表导出失败。{0}", err.Message));
                            }
                            finally
                            {
                                excelAccess.Close();
                            }
                        }
                        else
                        {
                            ExcelAccess excelAccess = new ExcelAccess();
                            try
                            {
                                excelAccess.Open(string.Format("{0}Template\\{1}", m_PathList[keHuBianHao], "出库物资单位汇总报表.xls"));
                                excelAccess.SetCellText(2, "A", string.Format("报表时间：{0}——{1}", kaiShiShiJian, jieShuShiJian));
                                for (int i = 0; i < Dt.Rows.Count; i++)
                                {
                                    excelAccess.SetCellText(i + 4, "A", string.Format("{0}", Dt.Rows[i]["danweimingcheng"]));
                                    excelAccess.SetCellText(i + 4, "B", string.Format("{0}", Dt.Rows[i]["wuliaomingcheng"]));
                                    excelAccess.SetCellText(i + 4, "C", string.Format("{0}", Dt.Rows[i]["xinghao"]));
                                    excelAccess.SetCellText(i + 4, "D", string.Format("{0}", Dt.Rows[i]["cheshu"]));
                                    excelAccess.SetCellText(i + 4, "E", string.Format("{0}", Dt.Rows[i]["jingzhong"]));
                                    excelAccess.SetCellText(i + 4, "F", string.Format("{0}", Dt.Rows[i]["kouzhong"]));
                                }

                                excelAccess.MergeCells(4 + Dt.Rows.Count, "A", 4 + Dt.Rows.Count, "F", true);
                                excelAccess.SetCellText(4 + Dt.Rows.Count, "A", string.Format("制表人：{0}    制表时间：{1}", currentYongHu, DateTime.Now.ToString("yyyy-MM-dd HH:mm")));

                                Excel.Range r = excelAccess.GetRange(4, "A", Dt.Rows.Count + 3, "F");
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlLeft);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlRight);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlBottom);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideHorizontal);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideVertical);

                                string fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], fileNameResult);
                                if (jieShuShiJian .Contains( DateTime.Today.ToString("yyyy-MM-dd")))
                                {
                                    fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], Guid.NewGuid().ToString());
                                }
                                excelAccess.SaveAs(fileName, true);

                                string strHeader = "attachment;filename=" + string.Format("{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmss"));//把文件头输出，此文件头激活文件下载框  
                                currentC.Response.AppendHeader("Content-Disposition", strHeader);//http报头文件   
                                currentC.Response.ContentType = "application/ms-excel";
                                byte[] bs = File.ReadAllBytes(fileName);
                                currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                                currentC.Response.Close();

                            }
                            catch (Exception err)
                            {
                                KeaoUtils.LogTxt(string.Format("出库单位汇总报表导出失败。{0}", err.Message));
                            }
                            finally
                            {
                                excelAccess.Close();
                            }
                        }

                    }
                    else
                    {
                        byte[] bs = Encoding.Default.GetBytes("<center>没有可导出的数据。</center>");
                        currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                        currentC.Response.Close();
                        return;
                    }

                }

                if (requestType == "4")
                {
                    //物料汇总
                    NameValueCollection queryString = HttpUtility.ParseQueryString(currentC.Request.RawUrl, Encoding.UTF8);
                    string zhanDian = string.Format("{0}", queryString["zhandian"]).Replace("_", "#");
                    string chengZhongLeiXing = string.Format("{0}", queryString["chengzhongleixing"]);
                    string kaiShiShiJian = string.Format("{0}", queryString["kaishishijian"]);
                    string jieShuShiJian = string.Format("{0}", queryString["jieshushijian"]);
                    string currentYongHu = string.Format("{0}", queryString["yonghu"]);

                    string createCondition = m_DB.CreateAndCondition(chengZhongLeiXing == "入库物料" ? "pizhongshijian" : "maozhongshijian", DataValueCommonType.日期时间, string.Format("{0} 00:00:00", kaiShiShiJian), string.Format("{0} 23:59:59", jieShuShiJian), SearchConditionType.between);
                    createCondition += m_DB.CreateAndCondition("chengzhongleixing", DataValueCommonType.字符串, chengZhongLeiXing, "", SearchConditionType.等于);
                    string tableCols = string.Empty;
                    string groupCols = string.Empty;
                    Dictionary<string, string> orderBy = new Dictionary<string, string>();
                    orderBy.Add("wuliaomingcheng", "asc");
                    orderBy.Add("xinghao", "asc");
                    tableCols = string.Format("wuliaomingcheng,xinghao,count(id) as cheshu,sum(maozhong) as maozhong,sum(pizhong) as pizhong,sum(kouzhong) as kouzhong,sum(jingzhong) as jingzhong,sum(fahuozhongliang) as fahuozhongliang,sum(jisuanfangliang) as jisuanfangliang", m_DB.DateFormatFun("maozhongshijian"));
                    groupCols = string.Format("wuliaomingcheng,xinghao", m_DB.DateFormatFun("maozhongshijian"));
                    DataTable Dt = m_DB.GetDataTableByCondition("tbl_chengzhong", tableCols, null, createCondition, groupCols, orderBy);
                    tableCols = string.Format("count(id) as cheshu,sum(maozhong) as maozhong,sum(pizhong) as pizhong,sum(kouzhong) as kouzhong,sum(jingzhong) as jingzhong,sum(fahuozhongliang) as fahuozhongliang,sum(jisuanfangliang) as jisuanfangliang", m_DB.DateFormatFun("maozhongshijian"));
                    DataTable DtCount = m_DB.GetDataTableByCondition("tbl_chengzhong", tableCols, null, createCondition, null, null);
                    if (Dt.Rows.Count > 0)
                    {
                        DataRow newRow = Dt.NewRow();
                        newRow["xinghao"] = "合计：";
                        newRow["cheshu"] = DtCount.Rows[0]["cheshu"];
                        newRow["maozhong"] = DtCount.Rows[0]["maozhong"];
                        newRow["pizhong"] = DtCount.Rows[0]["pizhong"];
                        newRow["kouzhong"] = DtCount.Rows[0]["kouzhong"];
                        newRow["jingzhong"] = DtCount.Rows[0]["jingzhong"];
                        newRow["fahuozhongliang"] = DtCount.Rows[0]["fahuozhongliang"];
                        newRow["jisuanfangliang"] = DtCount.Rows[0]["jisuanfangliang"];
                        Dt.Rows.Add(newRow);

                        if (chengZhongLeiXing == "入库物料")
                        {
                            ExcelAccess excelAccess = new ExcelAccess();
                            try
                            {
                                excelAccess.Open(string.Format("{0}Template\\{1}", m_PathList[keHuBianHao], "入库物资物料汇总报表.xls"));
                                excelAccess.SetCellText(2, "A", string.Format("报表时间：{0}——{1}", kaiShiShiJian, jieShuShiJian));
                                for (int i = 0; i < Dt.Rows.Count; i++)
                                {
                                    excelAccess.SetCellText(i + 4, "A", string.Format("{0}", Dt.Rows[i]["wuliaomingcheng"]));
                                    excelAccess.SetCellText(i + 4, "B", string.Format("{0}", Dt.Rows[i]["xinghao"]));
                                    excelAccess.SetCellText(i + 4, "C", string.Format("{0}", Dt.Rows[i]["cheshu"]));
                                    excelAccess.SetCellText(i + 4, "D", string.Format("{0}", Dt.Rows[i]["jingzhong"]));
                                    excelAccess.SetCellText(i + 4, "E", string.Format("{0}", Dt.Rows[i]["kouzhong"]));
                                    excelAccess.SetCellText(i + 4, "F", string.Format("{0}", Dt.Rows[i]["fahuozhongliang"]));
                                }

                                excelAccess.MergeCells(4 + Dt.Rows.Count, "A", 4 + Dt.Rows.Count, "F", true);
                                excelAccess.SetCellText(4 + Dt.Rows.Count, "A", string.Format("制表人：{0}    制表时间：{1}", currentYongHu, DateTime.Now.ToString("yyyy-MM-dd HH:mm")));

                                Excel.Range r = excelAccess.GetRange(4, "A", Dt.Rows.Count + 3, "F");
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlLeft);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlRight);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlBottom);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideHorizontal);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideVertical);

                                string fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], fileNameResult);
                                if (jieShuShiJian .Contains( DateTime.Today.ToString("yyyy-MM-dd")))
                                {
                                    fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], Guid.NewGuid().ToString());
                                }
                                excelAccess.SaveAs(fileName, true);

                                string strHeader = "attachment;filename=" + string.Format("{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmss"));//把文件头输出，此文件头激活文件下载框  
                                currentC.Response.AppendHeader("Content-Disposition", strHeader);//http报头文件   
                                currentC.Response.ContentType = "application/ms-excel";
                                byte[] bs = File.ReadAllBytes(fileName);
                                currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                                currentC.Response.Close();

                            }
                            catch (Exception err)
                            {
                                KeaoUtils.LogTxt(string.Format("入库物料汇总报表导出失败。{0}", err.Message));
                            }
                            finally
                            {
                                excelAccess.Close();
                            }
                        }
                        else
                        {
                            ExcelAccess excelAccess = new ExcelAccess();
                            try
                            {
                                excelAccess.Open(string.Format("{0}Template\\{1}", m_PathList[keHuBianHao], "出库物资物料汇总报表.xls"));
                                excelAccess.SetCellText(2, "A", string.Format("报表时间：{0}——{1}", kaiShiShiJian, jieShuShiJian));
                                for (int i = 0; i < Dt.Rows.Count; i++)
                                {
                                    excelAccess.SetCellText(i + 4, "A", string.Format("{0}", Dt.Rows[i]["wuliaomingcheng"]));
                                    excelAccess.SetCellText(i + 4, "B", string.Format("{0}", Dt.Rows[i]["xinghao"]));
                                    excelAccess.SetCellText(i + 4, "C", string.Format("{0}", Dt.Rows[i]["cheshu"]));
                                    excelAccess.SetCellText(i + 4, "D", string.Format("{0}", Dt.Rows[i]["jingzhong"]));
                                    excelAccess.SetCellText(i + 4, "E", string.Format("{0}", Dt.Rows[i]["kouzhong"]));
                                }

                                excelAccess.MergeCells(4 + Dt.Rows.Count, "A", 4 + Dt.Rows.Count, "E", true);
                                excelAccess.SetCellText(4 + Dt.Rows.Count, "A", string.Format("制表人：{0}    制表时间：{1}", currentYongHu, DateTime.Now.ToString("yyyy-MM-dd HH:mm")));

                                Excel.Range r = excelAccess.GetRange(4, "A", Dt.Rows.Count + 3, "E");
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlLeft);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlRight);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlBottom);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideHorizontal);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideVertical);

                                string fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], fileNameResult);
                                if (jieShuShiJian .Contains( DateTime.Today.ToString("yyyy-MM-dd")))
                                {
                                    fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], Guid.NewGuid().ToString());
                                }
                                excelAccess.SaveAs(fileName, true);

                                string strHeader = "attachment;filename=" + string.Format("{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmss"));//把文件头输出，此文件头激活文件下载框  
                                currentC.Response.AppendHeader("Content-Disposition", strHeader);//http报头文件   
                                currentC.Response.ContentType = "application/ms-excel";
                                byte[] bs = File.ReadAllBytes(fileName);
                                currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                                currentC.Response.Close();
                            }
                            catch (Exception err)
                            {
                                KeaoUtils.LogTxt(string.Format("出库物料汇总报表导出失败。{0}", err.Message));
                            }
                            finally
                            {
                                excelAccess.Close();
                            }
                        }
                    }
                    else
                    {
                        byte[] bs = Encoding.Default.GetBytes("<center>没有可导出的数据。</center>");
                        currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                        currentC.Response.Close();
                        return;
                    }
                }

                if (requestType == "5")
                {
                    //日结

                    NameValueCollection queryString = HttpUtility.ParseQueryString(currentC.Request.RawUrl, Encoding.UTF8);
                    string zhanDian = string.Format("{0}", queryString["zhandian"]).Replace("_", "#");
                    string chengZhongLeiXing = string.Format("{0}", queryString["chengzhongleixing"]);
                    string riJieRiQi = string.Format("{0}", queryString["rijieriqi"]);
                    string currentYongHu = string.Format("{0}", queryString["yonghu"]);

                    ExcelAccess excelAccess = new ExcelAccess();
                    try
                    {
                        DateTime kaiShi = DateTime.Parse(string.Format("{0} 00:00:00", riJieRiQi));
                        string createCondition = m_DB.CreateAndCondition("pizhongshijian", DataValueCommonType.日期时间, kaiShi.ToString("yyyy-MM-dd HH:mm:ss"), kaiShi.AddHours(23).AddMinutes(59).AddSeconds(59).ToString("yyyy-MM-dd HH:mm:ss"), SearchConditionType.between);
                        createCondition += m_DB.CreateAndCondition("chengzhongleixing", DataValueCommonType.字符串, chengZhongLeiXing, "", SearchConditionType.等于);
                        if (!string.IsNullOrEmpty(zhanDian) && zhanDian != "—全部—")
                        {
                            createCondition += m_DB.CreateAndCondition("zhandian", DataValueCommonType.字符串, zhanDian, "", SearchConditionType.等于);
                        }
                        DataTable Dt = m_DB.GetDataTableByCondition("tbl_chengzhong", "danweimingcheng,wuliaomingcheng,xinghao,count(id) as cheshu,sum(jingzhong) as jingzhong,sum(kouzhong) as kouzhong,sum(fahuozhongliang) as fahuozhongliang", null, createCondition, "danweimingcheng,wuliaomingcheng,xinghao", null);
                        if (Dt.Rows.Count > 0)
                        {

                            excelAccess.Open(string.Format("{0}Template\\{1}", m_PathList[keHuBianHao], "日结报表.xls"));
                            excelAccess.SetCellText(1, "A", string.Format("{0}日结报表", chengZhongLeiXing));
                            excelAccess.SetCellText(2, "A", string.Format("日期：{0}", kaiShi.ToString("yyyy-MM-dd")));
                            for (int i = 0; i < Dt.Rows.Count; i++)
                            {
                                excelAccess.SetCellText(i + 4, "A", string.Format("{0}", Dt.Rows[i]["danweimingcheng"]));
                                excelAccess.SetCellText(i + 4, "B", string.Format("{0}", Dt.Rows[i]["wuliaomingcheng"]));
                                excelAccess.SetCellText(i + 4, "C", string.Format("{0}", Dt.Rows[i]["xinghao"]));
                                excelAccess.SetCellText(i + 4, "D", string.Format("{0}", Dt.Rows[i]["cheshu"]));
                                excelAccess.SetCellText(i + 4, "E", string.Format("{0}", Dt.Rows[i]["jingzhong"]));
                                excelAccess.SetCellText(i + 4, "F", string.Format("{0}", Dt.Rows[i]["kouzhong"]));
                            }

                            excelAccess.MergeCells(4 + Dt.Rows.Count, "A", 4 + Dt.Rows.Count, "F", true);
                            excelAccess.SetCellText(4 + Dt.Rows.Count, "A", string.Format("制表人：{0}    制表时间：{1}", currentYongHu, DateTime.Now.ToString("yyyy-MM-dd HH:mm")));

                            Excel.Range r = excelAccess.GetRange(4, "A", Dt.Rows.Count + 3, "F");
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlLeft);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlRight);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlBottom);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideHorizontal);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideVertical);

                            string fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], fileNameResult);
                            if (riJieRiQi.Contains( DateTime.Today.ToString("yyyy-MM-dd")))
                            {
                                fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], Guid.NewGuid().ToString());
                            }
                            excelAccess.SaveAs(fileName, true);

                            string strHeader = "attachment;filename=" + string.Format("{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmss"));//把文件头输出，此文件头激活文件下载框  
                            currentC.Response.AppendHeader("Content-Disposition", strHeader);//http报头文件   
                            currentC.Response.ContentType = "application/ms-excel";
                            byte[] bs = File.ReadAllBytes(fileName);
                            currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                            currentC.Response.Close();
                        }
                        else
                        {
                            byte[] bs = Encoding.Default.GetBytes("<center>没有可导出的数据。</center>");
                            currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                            currentC.Response.Close();
                            return;
                        }

                    }
                    catch (Exception err)
                    {
                        KeaoUtils.LogTxt(string.Format("导出日结报表异常。{0}", err.Message));
                    }
                    finally
                    {
                        excelAccess.Close();
                    }
                }

                if (requestType == "6")
                {
                    //月结

                    NameValueCollection queryString = HttpUtility.ParseQueryString(currentC.Request.RawUrl, Encoding.UTF8);
                    string zhanDian = string.Format("{0}", queryString["zhandian"]).Replace("_", "#");
                    string chengZhongLeiXing = string.Format("{0}", queryString["chengzhongleixing"]);
                    string yueFen = string.Format("{0}", queryString["yuefen"]);
                    string currentYongHu = string.Format("{0}", queryString["yonghu"]);
                    string yueJie = "15";
                    //using (IRedisClient redis = RedisClientPool.GetReadOnlyRedisClient())
                    //{
                    //    if (redis != null)
                    //    {
                    //        yueJie = redis.GetValueFromHash(uid, "yuejie");
                    //    }
                    //}
                    //if (string.IsNullOrEmpty(yueJie))
                    //{
                    //    string createCondition = m_DB.CreateAndCondition("fenzu", DataValueCommonType.字符串, "月结", "", SearchConditionType.等于);
                    //    DataTable ziDianDt = m_DB.GetDataTableByCondition("tbl_zidian", createCondition);
                    //    yueJie = string.Format("{0}", ziDianDt.Rows[0]["xiangmu"]);
                    //}

                    ExcelAccess excelAccess = new ExcelAccess();
                    try
                    {
                        DateTime kaiShi = DateTime.Parse(string.Format("{0}-{1}-{2} 00:00:00", DateTime.Today.ToString("yyyy"), int.Parse(yueFen) - 1, yueJie));
                        DateTime jieShu = DateTime.Parse(string.Format("{0}-{1}-{2} 00:00:00", DateTime.Today.ToString("yyyy"), yueFen, yueJie));
                        string createCondition = m_DB.CreateAndCondition("pizhongshijian", DataValueCommonType.日期时间, kaiShi.ToString("yyyy-MM-dd HH:mm:ss"), jieShu.ToString("yyyy-MM-dd HH:mm:ss"), SearchConditionType.between);
                        createCondition += m_DB.CreateAndCondition("chengzhongleixing", DataValueCommonType.字符串, chengZhongLeiXing, "", SearchConditionType.等于);
                        if (!string.IsNullOrEmpty(zhanDian) && zhanDian != "—全部—")
                        {
                            createCondition += m_DB.CreateAndCondition("zhandian", DataValueCommonType.字符串, zhanDian, "", SearchConditionType.等于);
                        }
                        DataTable Dt = m_DB.GetDataTableByCondition("tbl_chengzhong", "danweimingcheng,wuliaomingcheng,xinghao,count(id) as cheshu,sum(jingzhong) as jingzhong,sum(kouzhong) as kouzhong,sum(fahuozhongliang) as fahuozhongliang", null, createCondition, "danweimingcheng,wuliaomingcheng,xinghao", null);
                        if (Dt.Rows.Count > 0)
                        {

                            excelAccess.Open(string.Format("{0}Template\\{1}", m_PathList[keHuBianHao], "月结报表.xls"));
                            excelAccess.SetCellText(1, "A", string.Format("{0}月结报表", chengZhongLeiXing));
                            excelAccess.SetCellText(2, "A", string.Format("月份：{0}", yueFen));
                            for (int i = 0; i < Dt.Rows.Count; i++)
                            {
                                excelAccess.SetCellText(i + 4, "A", string.Format("{0}", Dt.Rows[i]["danweimingcheng"]));
                                excelAccess.SetCellText(i + 4, "B", string.Format("{0}", Dt.Rows[i]["wuliaomingcheng"]));
                                excelAccess.SetCellText(i + 4, "C", string.Format("{0}", Dt.Rows[i]["xinghao"]));
                                excelAccess.SetCellText(i + 4, "D", string.Format("{0}", Dt.Rows[i]["cheshu"]));
                                excelAccess.SetCellText(i + 4, "E", string.Format("{0}", Dt.Rows[i]["jingzhong"]));
                                excelAccess.SetCellText(i + 4, "F", string.Format("{0}", Dt.Rows[i]["kouzhong"]));
                            }
                            excelAccess.MergeCells(4 + Dt.Rows.Count, "A", 4 + Dt.Rows.Count, "F", true);
                            excelAccess.SetCellText(4 + Dt.Rows.Count, "A", string.Format("制表人：{0}    制表时间：{1}", currentYongHu, DateTime.Now.ToString("yyyy-MM-dd HH:mm")));
                            Excel.Range r = excelAccess.GetRange(4, "A", Dt.Rows.Count + 3, "F");
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlLeft);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlRight);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlBottom);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideHorizontal);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideVertical);

                            string fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], fileNameResult);
                            if (yueFen == DateTime.Today.Month.ToString())
                            {
                                fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], Guid.NewGuid().ToString());
                            }
                            excelAccess.SaveAs(fileName, true);

                            string strHeader = "attachment;filename=" + string.Format("{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmss"));//把文件头输出，此文件头激活文件下载框  
                            currentC.Response.AppendHeader("Content-Disposition", strHeader);//http报头文件   
                            currentC.Response.ContentType = "application/ms-excel";
                            byte[] bs = File.ReadAllBytes(fileName);
                            currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                            currentC.Response.Close();
                        }
                        else
                        {
                            byte[] bs = Encoding.Default.GetBytes("<center>没有可导出的数据。</center>");
                            currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                            currentC.Response.Close();
                            return;
                        }
                    }
                    catch (Exception err)
                    {
                        KeaoUtils.LogTxt(string.Format("导出月结报表异常。{0}", err.Message));
                    }
                    finally
                    {
                        excelAccess.Close();
                    }
                }

                if (requestType == "7")
                {
                    //日志
                    NameValueCollection queryString = HttpUtility.ParseQueryString(currentC.Request.RawUrl, Encoding.UTF8);
                    string zhanDian = string.Format("{0}", queryString["zhandian"]).Replace("_", "#");
                    string kaiShiShiJian = string.Format("{0}", queryString["kaishishijian"]);
                    string jieShuShiJian = string.Format("{0}", queryString["jieshushijian"]);
                    string currentYongHu = string.Format("{0}", queryString["yonghu"]);

                    string createCondition = m_DB.CreateAndCondition("rizhishijian", DataValueCommonType.日期时间, string.Format("{0} 00:00:00", kaiShiShiJian), string.Format("{0} 23:59:59", jieShuShiJian), SearchConditionType.between);
                    createCondition += m_DB.CreateAndCondition("zhandian", DataValueCommonType.字符串, zhanDian, "", SearchConditionType.等于);
                    DataTable Dt = m_DB.GetDataTableByCondition("tbl_rizhi", createCondition);

                    if (Dt.Rows.Count > 0)
                    {
                        ExcelAccess excelAccess = new ExcelAccess();
                        try
                        {
                            excelAccess.Open(string.Format("{0}Template\\{1}", m_PathList[keHuBianHao], "日志.xls"));
                            excelAccess.SetCellText(2, "A", string.Format("报表时间：{0}——{1}", kaiShiShiJian, jieShuShiJian));
                            for (int i = 0; i < Dt.Rows.Count; i++)
                            {
                                excelAccess.SetCellText(i + 4, "A", string.Format("{0}", Dt.Rows[i]["caozuoyuan"]));
                                excelAccess.SetCellText(i + 4, "B", string.Format("{0}", Dt.Rows[i]["rizhishijian"]));
                                excelAccess.SetCellText(i + 4, "C", string.Format("{0}", Dt.Rows[i]["rizhineirong"]));
                            }

                            excelAccess.MergeCells(4 + Dt.Rows.Count, "A", 4 + Dt.Rows.Count, "C", true);
                            excelAccess.SetCellText(4 + Dt.Rows.Count, "A", string.Format("制表人：{0}    制表时间：{1}", currentYongHu, DateTime.Now.ToString("yyyy-MM-dd HH:mm")));

                            Excel.Range r = excelAccess.GetRange(4, "A", Dt.Rows.Count + 3, "C");
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlLeft);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlRight);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlBottom);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideHorizontal);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideVertical);

                            string fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao],fileNameResult);
                            if (jieShuShiJian .Contains( DateTime.Today.ToString("yyyy-MM-dd")))
                            {
                                fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], Guid.NewGuid().ToString());
                            }
                            excelAccess.SaveAs(fileName, true);

                            string strHeader = "attachment;filename=" + string.Format("{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmss"));//把文件头输出，此文件头激活文件下载框  
                            currentC.Response.AppendHeader("Content-Disposition", strHeader);//http报头文件   
                            currentC.Response.ContentType = "application/ms-excel";
                            byte[] bs = File.ReadAllBytes(fileName);
                            currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                            currentC.Response.Close();

                        }
                        catch (Exception err)
                        {
                            KeaoUtils.LogTxt(string.Format("日志导出失败。{0}", err.Message));
                        }
                        finally
                        {
                            excelAccess.Close();
                        }
                    }
                    else
                    {
                        byte[] bs = Encoding.Default.GetBytes("<center>没有可导出的数据。</center>");
                        currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                        currentC.Response.Close();
                        return;
                    }
                }

                if (requestType == "8")
                {
                    NameValueCollection queryString = HttpUtility.ParseQueryString(currentC.Request.RawUrl, Encoding.UTF8);
                    string zhanDian = string.Format("{0}", queryString["zhandian"]).Replace("_", "#");
                    string chengZhongLeiXing = string.Format("{0}", queryString["chengzhongleixing"]);
                    string kaiShiShiJian = string.Format("{0}", queryString["kaishishijian"]);
                    string jieShuShiJian = string.Format("{0}", queryString["jieshushijian"]);
                    string danWeiMingCheng = string.Format("{0}", queryString["danweimingcheng"]);
                    string wuLiaoMingCheng = string.Format("{0}", queryString["wuliaomingcheng"]);
                    string xingHao = string.Format("{0}", queryString["xinghao"]);
                    string chePaiHao = string.Format("{0}", queryString["chepaihao"]);
                    string currentYongHu = string.Format("{0}", queryString["yonghu"]);

                    string createCondition = m_DB.CreateAndCondition("tuikushijian", DataValueCommonType.日期时间, kaiShiShiJian, jieShuShiJian, SearchConditionType.between);
                    if (!string.IsNullOrEmpty(danWeiMingCheng) && danWeiMingCheng != "—全部—")
                    {
                        createCondition += m_DB.CreateAndCondition("tuikudanwei", DataValueCommonType.字符串, danWeiMingCheng, "", SearchConditionType.等于);
                    }
                    if (!string.IsNullOrEmpty(wuLiaoMingCheng) && wuLiaoMingCheng != "—全部—")
                    {
                        createCondition += m_DB.CreateAndCondition("tuikuwuliao", DataValueCommonType.字符串, wuLiaoMingCheng, "", SearchConditionType.等于);
                    }
                    if (!string.IsNullOrEmpty(xingHao) && xingHao != "—全部—")
                    {
                        createCondition += m_DB.CreateAndCondition("tuikuxinghao", DataValueCommonType.字符串, xingHao, "", SearchConditionType.等于);
                    }
                    if (!string.IsNullOrEmpty(chePaiHao))
                    {
                        createCondition += m_DB.CreateAndCondition("tuikuchepaihao", DataValueCommonType.字符串, chePaiHao, "", SearchConditionType.like);
                    }
                    createCondition += m_DB.CreateAndCondition("tuikuleixing", DataValueCommonType.字符串, chengZhongLeiXing, "", SearchConditionType.等于);
                    Dictionary<string, string> orderBy = new Dictionary<string, string>();
                    orderBy.Add("tuikushijian", "desc");
                    DataTable Dt = m_DB.GetDataTableByCondition("tbl_tuiku", createCondition, orderBy);
                    DataTable DtCount = m_DB.GetDataTableByCondition("tbl_tuiku", "count(id) as cheshu,sum(tuikumaozhong) as tuikumaozhong,sum(tuikupizhong) as tuikupizhong,sum(tuikujingzhong) as tuikujingzhong", null, createCondition, null, null);
                    if (Dt.Rows.Count > 0)
                    {
                        DataRow newRow = Dt.NewRow();
                        newRow["tuikuchepaihao"] = string.Format("合计：{0}车", DtCount.Rows[0]["cheshu"]);
                        newRow["tuikumaozhong"] = DtCount.Rows[0]["tuikumaozhong"];
                        newRow["tuikupizhong"] = DtCount.Rows[0]["tuikupizhong"];
                        newRow["tuikujingzhong"] = DtCount.Rows[0]["tuikujingzhong"];
                        Dt.Rows.Add(newRow);

                        if (chengZhongLeiXing == "入库物料")
                        {
                            ExcelAccess excelAccess = new ExcelAccess();
                            try
                            {
                                excelAccess.Open(string.Format("{0}Template\\{1}", m_PathList[keHuBianHao], "入库物资退库明细.xls"));
                                excelAccess.SetCellText(2, "A", string.Format("报表时间：{0}——{1}", kaiShiShiJian, jieShuShiJian));
                                for (int i = 0; i < Dt.Rows.Count; i++)
                                {
                                    excelAccess.SetCellText(i + 4, "A", string.Format("{0}", Dt.Rows[i]["tuikubianhao"]));
                                    excelAccess.SetCellText(i + 4, "B", string.Format("{0}", Dt.Rows[i]["tuikuchepaihao"]));
                                    excelAccess.SetCellText(i + 4, "C", string.Format("{0}", Dt.Rows[i]["tuikudanwei"]));
                                    excelAccess.SetCellText(i + 4, "D", string.Format("{0}", Dt.Rows[i]["tuikuwuliao"]));
                                    excelAccess.SetCellText(i + 4, "E", string.Format("{0}", Dt.Rows[i]["tuikuxinghao"]));
                                    excelAccess.SetCellText(i + 4, "F", string.Format("{0}", Dt.Rows[i]["tuikushijian"]));
                                    excelAccess.SetCellText(i + 4, "G", string.Format("{0}", Dt.Rows[i]["tuikujingzhong"]));
                                }

                                excelAccess.MergeCells(4 + Dt.Rows.Count, "A", 4 + Dt.Rows.Count, "G", true);
                                excelAccess.SetCellText(4 + Dt.Rows.Count, "A", string.Format("制表人：{0}    制表时间：{1}", currentYongHu, DateTime.Now.ToString("yyyy-MM-dd HH:mm")));

                                Excel.Range r = excelAccess.GetRange(4, "A", Dt.Rows.Count + 3, "G");
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlLeft);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlRight);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlBottom);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideHorizontal);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideVertical);

                                string fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], fileNameResult);
                                if (jieShuShiJian .Contains( DateTime.Today.ToString("yyyy-MM-dd")))
                                {
                                    fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], Guid.NewGuid().ToString());
                                }
                                excelAccess.SaveAs(fileName, true);

                                string strHeader = "attachment;filename=" + string.Format("{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmss"));//把文件头输出，此文件头激活文件下载框  
                                currentC.Response.AppendHeader("Content-Disposition", strHeader);//http报头文件   
                                currentC.Response.ContentType = "application/ms-excel";
                                byte[] bs = File.ReadAllBytes(fileName);
                                currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                                currentC.Response.Close();

                            }
                            catch (Exception err)
                            {
                                KeaoUtils.LogTxt(string.Format("入库物资退库明细报表导出失败。{0}", err.Message));
                            }
                            finally
                            {
                                excelAccess.Close();
                            }
                        }
                        else
                        {
                            ExcelAccess excelAccess = new ExcelAccess();
                            try
                            {
                                excelAccess.Open(string.Format("{0}Template\\{1}", m_PathList[keHuBianHao], "出库物资退库明细.xls"));
                                excelAccess.SetCellText(2, "A", string.Format("报表时间：{0}——{1}", kaiShiShiJian, jieShuShiJian));
                                for (int i = 0; i < Dt.Rows.Count; i++)
                                {
                                    excelAccess.SetCellText(i + 4, "A", string.Format("{0}", Dt.Rows[i]["tuikubianhao"]));
                                    excelAccess.SetCellText(i + 4, "B", string.Format("{0}", Dt.Rows[i]["tuikuchepaihao"]));
                                    excelAccess.SetCellText(i + 4, "C", string.Format("{0}", Dt.Rows[i]["tuikudanwei"]));
                                    excelAccess.SetCellText(i + 4, "D", string.Format("{0}", Dt.Rows[i]["tuikuwuliao"]));
                                    excelAccess.SetCellText(i + 4, "E", string.Format("{0}", Dt.Rows[i]["tuikuxinghao"]));
                                    excelAccess.SetCellText(i + 4, "F", string.Format("{0}", Dt.Rows[i]["tuikushijian"]));
                                    excelAccess.SetCellText(i + 4, "G", string.Format("{0}", Dt.Rows[i]["tuikujingzhong"]));
                                    excelAccess.SetCellText(i + 4, "H", string.Format("{0}", Dt.Rows[i]["tuikubiaoduan"]));
                                }

                                excelAccess.MergeCells(4 + Dt.Rows.Count, "A", 4 + Dt.Rows.Count, "H", true);
                                excelAccess.SetCellText(4 + Dt.Rows.Count, "A", string.Format("制表人：{0}    制表时间：{1}", currentYongHu, DateTime.Now.ToString("yyyy-MM-dd HH:mm")));

                                Excel.Range r = excelAccess.GetRange(4, "A", Dt.Rows.Count + 3, "H");
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlLeft);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlRight);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlBottom);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideHorizontal);
                                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideVertical);

                                string fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], fileNameResult);
                                if (jieShuShiJian .Contains( DateTime.Today.ToString("yyyy-MM-dd")))
                                {
                                    fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], Guid.NewGuid().ToString());
                                }
                                excelAccess.SaveAs(fileName, true);

                                string strHeader = "attachment;filename=" + string.Format("{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmss"));//把文件头输出，此文件头激活文件下载框  
                                currentC.Response.AppendHeader("Content-Disposition", strHeader);//http报头文件   
                                currentC.Response.ContentType = "application/ms-excel";
                                byte[] bs = File.ReadAllBytes(fileName);
                                currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                                currentC.Response.Close();
                            }
                            catch (Exception err)
                            {
                                KeaoUtils.LogTxt(string.Format("出库物资退库明细报表导出失败。{0}", err.Message));
                            }
                            finally
                            {
                                excelAccess.Close();
                            }
                        }
                    }
                    else
                    {
                        byte[] bs = Encoding.Default.GetBytes("<center>没有可导出的数据。</center>");
                        currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                        currentC.Response.Close();
                        return;
                    }
                }

                if (requestType == "9")
                {
                    //混合料汇总
                    NameValueCollection queryString = HttpUtility.ParseQueryString(currentC.Request.RawUrl, Encoding.UTF8);
                    string zhanDian = string.Format("{0}", queryString["zhandian"]).Replace("_", "#");
                    string kaishi = string.Format("{0}", queryString["kaishi"]);
                    string jieshu = string.Format("{0}", queryString["jieshu"]);
                    string currentYongHu = string.Format("{0}", queryString["yonghu"]);

                    object param1 = m_DB.CreateParamter("@kaishi", string.Format("{0} 00:00:00", kaishi), false);
                    object param2 = m_DB.CreateParamter("@jieshu", string.Format("{0} 23:59:59", jieshu), false);
                    object param3 = m_DB.CreateParamter("@zhandian", string.Format("{0}", zhanDian), false);
                    DataSet Ds = m_DB.ExecuteTableProcedure("P_ShuiWenLiao_HunHeLiaoShiGong", new object[] { param1, param2, param3 });

                    if (Ds.Tables.Count > 0 && Ds.Tables[0].Rows.Count > 0)
                    {
                        DataRow newRow = Ds.Tables[0].NewRow();
                        newRow["jihualiang"] = Ds.Tables[1].Rows[0]["jihualiang"];
                        newRow["jingzhong"] = Ds.Tables[1].Rows[0]["jingzhong"];
                        newRow["shigongganzhong"] = Ds.Tables[1].Rows[0]["shigongganzhong"];
                        newRow["wuchaliang"] = Ds.Tables[1].Rows[0]["wuchaliang"];
                        newRow["yingkui"] = Ds.Tables[1].Rows[0]["yingkui"];
                        Ds.Tables[0].Rows.Add(newRow);
                        DataTable Dt = Ds.Tables[0];
                        ExcelAccess excelAccess = new ExcelAccess();
                        try
                        {
                            excelAccess.Open(string.Format("{0}Template\\{1}", m_PathList[keHuBianHao], "核算_混合料.xls"));
                            excelAccess.SetCellText(2, "A", string.Format("报表时间：{0}——{1}", kaishi, jieshu));
                            for (int i = 0; i < Dt.Rows.Count; i++)
                            {
                                excelAccess.SetCellText(i + 4, "A", string.Format("{0}", Dt.Rows[i]["riqi"]));
                                excelAccess.SetCellText(i + 4, "B", string.Format("{0}", Dt.Rows[i]["biaoduanmingcheng"]));
                                excelAccess.SetCellText(i + 4, "C", string.Format("{0}", Dt.Rows[i]["gongcheng"]));
                                excelAccess.SetCellText(i + 4, "D", string.Format("{0}", Dt.Rows[i]["zuoyoufu"]));
                                excelAccess.SetCellText(i + 4, "E", string.Format("{0}", Dt.Rows[i]["biaoduanchangdu"]));
                                excelAccess.SetCellText(i + 4, "F", string.Format("{0}", Dt.Rows[i]["biaoduankuandu"]));
                                excelAccess.SetCellText(i + 4, "G", string.Format("{0}", Dt.Rows[i]["biaoduanhoudu"]));
                                excelAccess.SetCellText(i + 4, "H", string.Format("{0}", Dt.Rows[i]["ganmidu"]));
                                excelAccess.SetCellText(i + 4, "I", string.Format("{0}", Dt.Rows[i]["jihualiang"]));
                                excelAccess.SetCellText(i + 4, "J", string.Format("{0}", Dt.Rows[i]["jingzhong"]));
                                excelAccess.SetCellText(i + 4, "K", string.Format("{0}", Dt.Rows[i]["hanshuiliang"]));
                                excelAccess.SetCellText(i + 4, "L", string.Format("{0}", Dt.Rows[i]["shigongganzhong"]));
                                excelAccess.SetCellText(i + 4, "M", string.Format("{0}", Dt.Rows[i]["wuchaliang"]));
                            }

                            excelAccess.MergeCells(4 + Dt.Rows.Count, "A", 4 + Dt.Rows.Count, "M", true);
                            excelAccess.SetCellText(4 + Dt.Rows.Count, "A", string.Format("制表人：{0}    制表时间：{1}", currentYongHu, DateTime.Now.ToString("yyyy-MM-dd HH:mm")));

                            Excel.Range r = excelAccess.GetRange(4, "A", Dt.Rows.Count + 3, "M");
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlLeft);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlRight);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlBottom);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideHorizontal);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideVertical);

                            string fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], fileNameResult);
                            if (jieshu.Contains(DateTime.Today.ToString("yyyy-MM-dd")))
                            {
                                fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], Guid.NewGuid().ToString());
                            }
                            excelAccess.SaveAs(fileName, true);

                            string strHeader = "attachment;filename=" + string.Format("{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmss"));//把文件头输出，此文件头激活文件下载框  
                            currentC.Response.AppendHeader("Content-Disposition", strHeader);//http报头文件   
                            currentC.Response.ContentType = "application/ms-excel";
                            byte[] bs = File.ReadAllBytes(fileName);
                            currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                            currentC.Response.Close();
                        }
                        catch (Exception err)
                        {
                            KeaoUtils.LogTxt(string.Format("混合料核算报表导出失败。{0}", err.Message));
                        }
                        finally
                        {
                            excelAccess.Close();
                        }
                    }
                    else
                    {
                        byte[] bs = Encoding.Default.GetBytes("<center>没有可导出的数据。</center>");
                        currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                        currentC.Response.Close();
                        return;
                    }
                }

                if (requestType == "10")
                {
                    //账面库存查询
                    NameValueCollection queryString = HttpUtility.ParseQueryString(currentC.Request.RawUrl, Encoding.UTF8);
                    string zhandian = string.Format("{0}", queryString["zhandian"]).Replace("_", "#");
                    string riqi = string.Format("{0}", queryString["kaishi"]);
                    string currentYongHu = string.Format("{0}", queryString["yonghu"]);

                    string createCondition = m_DB.CreateAndCondition("riqi", DataValueCommonType.日期时间, string.Format("{0} 00:00:00", riqi), string.Format("{0} 23:59:59", riqi), SearchConditionType.between);
                    if (!string.IsNullOrEmpty(zhandian) && zhandian != "—全部—")
                    {
                        createCondition += m_DB.CreateAndCondition("zhandian", DataValueCommonType.字符串, zhandian, "", SearchConditionType.等于);
                    }
                    Dictionary<string, string> orderBys = new Dictionary<string, string>();
                    orderBys.Add("wuliaomingcheng", "asc");
                    orderBys.Add("xinghaomingcheng", "asc");
                    DataTable Dt = m_DB.GetDataTableByCondition("tbl_kucun", createCondition, orderBys);

                    if (Dt.Rows.Count > 0)
                    {
                        ExcelAccess excelAccess = new ExcelAccess();
                        try
                        {
                            excelAccess.Open(string.Format("{0}Template\\{1}", m_PathList[keHuBianHao], "核算_账面库存.xls"));
                            excelAccess.SetCellText(2, "A", string.Format("报表时间：{0}  站点：{1}", riqi,zhandian));
                            for (int i = 0; i < Dt.Rows.Count; i++)
                            {
                                excelAccess.SetCellText(i + 4, "A", string.Format("{0}", Dt.Rows[i]["riqi"]).Substring(0,10));
                                excelAccess.SetCellText(i + 4, "B", string.Format("{0}", Dt.Rows[i]["wuliaomingcheng"]));
                                excelAccess.SetCellText(i + 4, "C", string.Format("{0}", Dt.Rows[i]["xinghaomingcheng"]));
                                excelAccess.SetCellText(i + 4, "D", string.Format("{0}", Dt.Rows[i]["jieyu"]));
                                excelAccess.SetCellText(i + 4, "E", string.Format("{0}", Dt.Rows[i]["ruku"]));
                                excelAccess.SetCellText(i + 4, "F", string.Format("{0}", Dt.Rows[i]["chuku"]));
                                excelAccess.SetCellText(i + 4, "G", string.Format("{0}", Dt.Rows[i]["tiaocha"]));
                                excelAccess.SetCellText(i + 4, "H", string.Format("{0}", Dt.Rows[i]["kucun"]));
                            }

                            excelAccess.MergeCells(4 + Dt.Rows.Count, "A", 4 + Dt.Rows.Count, "H", true);
                            excelAccess.SetCellText(4 + Dt.Rows.Count, "A", string.Format("制表人：{0}    制表时间：{1}", currentYongHu, DateTime.Now.ToString("yyyy-MM-dd HH:mm")));

                            Excel.Range r = excelAccess.GetRange(4, "A", Dt.Rows.Count + 3, "H");
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlLeft);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlRight);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlBottom);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideHorizontal);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideVertical);

                            string fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], fileNameResult);
                            if (riqi.Contains(DateTime.Today.ToString("yyyy-MM-dd")))
                            {
                                fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], Guid.NewGuid().ToString());
                            }
                            excelAccess.SaveAs(fileName, true);

                            string strHeader = "attachment;filename=" + string.Format("{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmss"));//把文件头输出，此文件头激活文件下载框  
                            currentC.Response.AppendHeader("Content-Disposition", strHeader);//http报头文件   
                            currentC.Response.ContentType = "application/ms-excel";
                            byte[] bs = File.ReadAllBytes(fileName);
                            currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                            currentC.Response.Close();
                        }
                        catch (Exception err)
                        {
                            KeaoUtils.LogTxt(string.Format("账面库存报表导出失败。{0}", err.Message));
                        }
                        finally
                        {
                            excelAccess.Close();
                        }
                    }
                    else
                    {
                        byte[] bs = Encoding.Default.GetBytes("<center>没有可导出的数据。</center>");
                        currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                        currentC.Response.Close();
                        return;
                    }
                }

                if (requestType == "11")
                {
                    //核算碎石盘点报表
                    NameValueCollection queryString = HttpUtility.ParseQueryString(currentC.Request.RawUrl, Encoding.UTF8);
                    string zhandian = string.Format("{0}", queryString["zhandian"]).Replace("_", "#");
                    string kaishi = string.Format("{0}", queryString["kaishi"]);
                    string jieshu = string.Format("{0}", queryString["jieshu"]);
                    string currentYongHu = string.Format("{0}", queryString["yonghu"]);
                    object[] SqlParams1 = null;
                    SqlParams1 = new object[] {
                               m_DB.CreateParamter("@kaishishijian",string.Format("{0} 00:00:00",kaishi),false),
                               m_DB.CreateParamter("@jieshushijian",string.Format("{0} 23:59:59",jieshu),false),
                               m_DB.CreateParamter("@zhandian",string.Format("{0}",zhandian),false)
                        };
                    DataSet Ds1 = m_DB.ExecuteTableProcedure("P_HeSuan_SuiShi", SqlParams1);
                    if (Ds1.Tables.Count > 0 && Ds1.Tables[0].Rows.Count > 0)
                    {
                        for (int i = 0; i < Ds1.Tables[0].Rows.Count; i++)
                        {
                            if (i == Ds1.Tables[0].Rows.Count - 1)
                            {
                                foreach (DataRow heJiRow in Ds1.Tables[1].Rows)
                                {
                                    if (heJiRow["wuliaomingcheng"].Equals(Ds1.Tables[0].Rows[i]["wuliaomingcheng"]) && heJiRow["xinghaomingcheng"].Equals(Ds1.Tables[0].Rows[i]["xinghaomingcheng"]))
                                    {
                                        DataRow newRow = Ds1.Tables[0].NewRow();
                                        newRow["xinghaomingcheng"] = "合计：";
                                        newRow["pandianxiaohao"] = heJiRow["pandianxiaohao"];
                                        newRow["shejixiaohao"] = heJiRow["shejixiaohao"];
                                        newRow["yingkui"] = heJiRow["yingkui"];
                                        i++;
                                        Ds1.Tables[0].Rows.InsertAt(newRow, i);
                                        break;
                                    }
                                }
                                break;
                            }

                            if (!Ds1.Tables[0].Rows[i]["wuliaomingcheng"].Equals(Ds1.Tables[0].Rows[i + 1]["wuliaomingcheng"]) || !Ds1.Tables[0].Rows[i]["xinghaomingcheng"].Equals(Ds1.Tables[0].Rows[i + 1]["xinghaomingcheng"]))
                            {
                                foreach (DataRow heJiRow in Ds1.Tables[1].Rows)
                                {
                                    if (heJiRow["wuliaomingcheng"].Equals(Ds1.Tables[0].Rows[i]["wuliaomingcheng"]) && heJiRow["xinghaomingcheng"].Equals(Ds1.Tables[0].Rows[i]["xinghaomingcheng"]))
                                    {
                                        DataRow newRow = Ds1.Tables[0].NewRow();
                                        newRow["xinghaomingcheng"] = "合计：";
                                        newRow["pandianxiaohao"] = heJiRow["pandianxiaohao"];
                                        newRow["shejixiaohao"] = heJiRow["shejixiaohao"];
                                        newRow["yingkui"] = heJiRow["yingkui"];
                                        i++;
                                        Ds1.Tables[0].Rows.InsertAt(newRow, i);
                                        break;
                                    }
                                }
                            }
                        }

                        ExcelAccess excelAccess = new ExcelAccess();
                        try
                        {
                            DataTable Dt = Ds1.Tables[0];
                            excelAccess.Open(string.Format("{0}Template\\{1}", m_PathList[keHuBianHao], "核算_碎石核算.xls"));
                            excelAccess.SetCellText(2, "A", string.Format("报表时间：{0}——{1}   站点：{2}", kaishi,jieshu,zhandian));
                            for (int i = 0; i < Dt.Rows.Count; i++)
                            {
                                excelAccess.SetCellText(i + 4, "A", string.Format("{0}", Dt.Rows[i]["pandianriqi"]).Length>10?string.Format("{0}", Dt.Rows[i]["pandianriqi"]).Substring(0,10): string.Format("{0}", Dt.Rows[i]["pandianriqi"]));
                                excelAccess.SetCellText(i + 4, "B", string.Format("{0}", Dt.Rows[i]["wuliaomingcheng"]));
                                excelAccess.SetCellText(i + 4, "C", string.Format("{0}", Dt.Rows[i]["xinghaomingcheng"]));
                                excelAccess.SetCellText(i + 4, "D", string.Format("{0}", Dt.Rows[i]["pandianxiaohao"]));
                                excelAccess.SetCellText(i + 4, "E", string.Format("{0}", Dt.Rows[i]["shejixiaohao"]));
                                excelAccess.SetCellText(i + 4, "F", string.Format("{0}", Dt.Rows[i]["yingkui"]));
                            }

                            excelAccess.MergeCells(4 + Dt.Rows.Count, "A", 4 + Dt.Rows.Count, "F", true);
                            excelAccess.SetCellText(4 + Dt.Rows.Count, "A", string.Format("制表人：{0}    制表时间：{1}", currentYongHu, DateTime.Now.ToString("yyyy-MM-dd HH:mm")));

                            Excel.Range r = excelAccess.GetRange(4, "A", Dt.Rows.Count + 3, "F");
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlLeft);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlRight);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlBottom);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideHorizontal);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideVertical);

                            string fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], fileNameResult);
                            if (jieshu.Contains(DateTime.Today.ToString("yyyy-MM-dd")))
                            {
                                fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], Guid.NewGuid().ToString());
                            }
                            excelAccess.SaveAs(fileName, true);

                            string strHeader = "attachment;filename=" + string.Format("{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmss"));//把文件头输出，此文件头激活文件下载框  
                            currentC.Response.AppendHeader("Content-Disposition", strHeader);//http报头文件   
                            currentC.Response.ContentType = "application/ms-excel";
                            byte[] bs = File.ReadAllBytes(fileName);
                            currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                            currentC.Response.Close();
                        }
                        catch (Exception err)
                        {
                            KeaoUtils.LogTxt(string.Format("碎石盘点核算报表导出失败。{0}", err.Message));
                        }
                        finally
                        {
                            excelAccess.Close();
                        }
                    }
                    else
                    {
                        byte[] bs = Encoding.Default.GetBytes("<center>没有可导出的数据。</center>");
                        currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                        currentC.Response.Close();
                        return;
                    }
                }

                if (requestType == "12")
                {
                    NameValueCollection queryString = HttpUtility.ParseQueryString(currentC.Request.RawUrl, Encoding.UTF8);
                    string zhandian = string.Format("{0}", queryString["zhandian"]).Replace("_", "#");
                    string kaishi = string.Format("{0}", queryString["kaishi"]);
                    string jieshu = string.Format("{0}", queryString["jieshu"]);
                    string currentYongHu = string.Format("{0}", queryString["yonghu"]);

                    object[] SqlParams1 = new object[] {
                               m_DB.CreateParamter("@kaishishijian",string.Format("{0} 00:00:00",kaishi),false),
                               m_DB.CreateParamter("@jieshushijian",string.Format("{0} 23:59:59",jieshu),false),
                               m_DB.CreateParamter("@zhandian",string.Format("{0}",zhandian),false)
                        };
                    DataSet Ds1 = m_DB.ExecuteTableProcedure("P_HeSuan_ShuiNi", SqlParams1);
                    if (Ds1.Tables.Count > 0 && Ds1.Tables[0].Rows.Count > 0)
                    {
                        DataRow newRow = Ds1.Tables[0].NewRow();
                        newRow["shuiwenliaoshejiliang"] = Ds1.Tables[1].Rows[0]["shuiwenliaoshejiliang"];
                        newRow["shejixiaohao"] = Ds1.Tables[1].Rows[0]["shejixiaohao"];
                        newRow["pandianxiaohao"] = Ds1.Tables[1].Rows[0]["pandianxiaohao"];
                        newRow["yingkui"] = Ds1.Tables[1].Rows[0]["yingkui"];
                        newRow["yingkuibili"] = Ds1.Tables[1].Rows[0]["yingkuibili"];
                        newRow["shuiwenliaochukuganzhong"] = Ds1.Tables[1].Rows[0]["shuiwenliaochukuganzhong"];
                        newRow["shigongshuinijiliang"] = Ds1.Tables[1].Rows[0]["shigongshuinijiliang"];
                        Ds1.Tables[0].Rows.Add(newRow);


                        ExcelAccess excelAccess = new ExcelAccess();
                        try
                        {
                            DataTable Dt = Ds1.Tables[0];
                            excelAccess.Open(string.Format("{0}Template\\{1}", m_PathList[keHuBianHao], "核算_水泥核算.xls"));
                            excelAccess.SetCellText(2, "A", string.Format("报表时间：{0}——{1}   站点：{2}", kaishi, jieshu,zhandian));
                            for (int i = 0; i < Dt.Rows.Count; i++)
                            {
                                excelAccess.SetCellText(i + 4, "A", string.Format("{0}", Dt.Rows[i]["pandianriqi"]).Length > 10 ? string.Format("{0}", Dt.Rows[i]["pandianriqi"]).Substring(0, 10) : "合计：");
                                excelAccess.SetCellText(i + 4, "B", string.Format("{0}", Dt.Rows[i]["shuiwenliaoshejiliang"]));
                                excelAccess.SetCellText(i + 4, "C", string.Format("{0}", Dt.Rows[i]["shejixiaohao"]));
                                excelAccess.SetCellText(i + 4, "D", string.Format("{0}", Dt.Rows[i]["pandianxiaohao"]));
                                excelAccess.SetCellText(i + 4, "E", string.Format("{0}", Dt.Rows[i]["yingkui"]));
                                excelAccess.SetCellText(i + 4, "F", string.Format("{0}", Dt.Rows[i]["yingkuibili"]));
                                excelAccess.SetCellText(i + 4, "G", string.Format("{0}", Dt.Rows[i]["shuiwenliaochukuganzhong"]));
                                excelAccess.SetCellText(i + 4, "H", string.Format("{0}", Dt.Rows[i]["shigongshuinijiliang"]));
                            }

                            excelAccess.MergeCells(4 + Dt.Rows.Count, "A", 4 + Dt.Rows.Count, "H", true);
                            excelAccess.SetCellText(4 + Dt.Rows.Count, "A", string.Format("制表人：{0}    制表时间：{1}", currentYongHu, DateTime.Now.ToString("yyyy-MM-dd HH:mm")));

                            Excel.Range r = excelAccess.GetRange(4, "A", Dt.Rows.Count + 3, "H");
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlLeft);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlRight);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlBottom);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideHorizontal);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideVertical);

                            string fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], fileNameResult);
                            if (jieshu.Contains(DateTime.Today.ToString("yyyy-MM-dd")))
                            {
                                fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], Guid.NewGuid().ToString());
                            }
                            excelAccess.SaveAs(fileName, true);

                            string strHeader = "attachment;filename=" + string.Format("{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmss"));//把文件头输出，此文件头激活文件下载框  
                            currentC.Response.AppendHeader("Content-Disposition", strHeader);//http报头文件   
                            currentC.Response.ContentType = "application/ms-excel";
                            byte[] bs = File.ReadAllBytes(fileName);
                            currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                            currentC.Response.Close();
                        }
                        catch (Exception err)
                        {
                            KeaoUtils.LogTxt(string.Format("水泥盘点核算报表导出失败。{0}", err.Message));
                        }
                        finally
                        {
                            excelAccess.Close();
                        }

                    }
                    else
                    {
                        byte[] bs = Encoding.Default.GetBytes("<center>没有可导出的数据。</center>");
                        currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                        currentC.Response.Close();
                        return;
                    }

                }

                if (requestType == "13")
                {
                    //材料盘点导出
                    NameValueCollection queryString = HttpUtility.ParseQueryString(currentC.Request.RawUrl, Encoding.UTF8);
                    string zhandian = string.Format("{0}", queryString["zhandian"]).Replace("_", "#");
                    string kaishi = string.Format("{0}", queryString["kaishi"]);
                    string jieshu = string.Format("{0}", queryString["jieshu"]);
                    string currentYongHu = string.Format("{0}", queryString["yonghu"]);

                    object[] SearchParmas = new object[] {
                            m_DB.CreateParamter("@zhandian",zhandian,false),
                            m_DB.CreateParamter("@pandianriqi_kaishi",string.Format("{0} 00:00:00",kaishi),false),
                            m_DB.CreateParamter("@pandianriqi_jieshu",string.Format("{0} 23:59:59",jieshu),false)};

                    DataSet Ds = m_DB.ExecuteTableProcedure("P_XinXi_ShuiWenLiao_PanDian", SearchParmas);
                    if (Ds != null && Ds.Tables.Count > 0 && Ds.Tables[0].Rows.Count > 0)
                    {
                        for (int i = 0; i < Ds.Tables[0].Rows.Count; i++)
                        {
                            if (i == Ds.Tables[0].Rows.Count - 1)
                            {
                                foreach (DataRow heJiRow in Ds.Tables[1].Rows)
                                {
                                    if (heJiRow["wuliaomingcheng"].Equals(Ds.Tables[0].Rows[i]["wuliaomingcheng"]) && heJiRow["xinghaomingcheng"].Equals(Ds.Tables[0].Rows[i]["xinghaomingcheng"]))
                                    {
                                        DataRow newRow = Ds.Tables[0].NewRow();
                                        newRow["xinghaomingcheng"] = "合计：";
                                        newRow["benqijinchang"] = heJiRow["benqijinchang"];
                                        newRow["benqixiaohao"] = heJiRow["benqixiaohao"];
                                        i++;
                                        Ds.Tables[0].Rows.InsertAt(newRow, i);
                                        break;
                                    }
                                }
                                break;
                            }

                            if (!Ds.Tables[0].Rows[i]["wuliaomingcheng"].Equals(Ds.Tables[0].Rows[i + 1]["wuliaomingcheng"]) || !Ds.Tables[0].Rows[i]["xinghaomingcheng"].Equals(Ds.Tables[0].Rows[i + 1]["xinghaomingcheng"]))
                            {
                                foreach (DataRow heJiRow in Ds.Tables[1].Rows)
                                {
                                    if (heJiRow["wuliaomingcheng"].Equals(Ds.Tables[0].Rows[i]["wuliaomingcheng"]) && heJiRow["xinghaomingcheng"].Equals(Ds.Tables[0].Rows[i]["xinghaomingcheng"]))
                                    {
                                        DataRow newRow = Ds.Tables[0].NewRow();
                                        newRow["xinghaomingcheng"] = "合计：";
                                        newRow["benqijinchang"] = heJiRow["benqijinchang"];
                                        newRow["benqixiaohao"] = heJiRow["benqixiaohao"];
                                        i++;
                                        Ds.Tables[0].Rows.InsertAt(newRow, i);
                                        break;
                                    }
                                }
                            }
                        }

                        ExcelAccess excelAccess = new ExcelAccess();
                        try
                        {
                            DataTable Dt = Ds.Tables[0];
                            excelAccess.Open(string.Format("{0}Template\\{1}", m_PathList[keHuBianHao], "核算_盘点.xls"));
                            excelAccess.SetCellText(2, "A", string.Format("报表时间：{0}——{1}   站点：{2}", kaishi, jieshu,zhandian));
                            for (int i = 0; i < Dt.Rows.Count; i++)
                            {
                                excelAccess.SetCellText(i + 4, "A", string.Format("{0}", Dt.Rows[i]["jieshuriqi"]).Length > 10 ? string.Format("{0}", Dt.Rows[i]["jieshuriqi"]).Substring(0, 10) : "");
                                excelAccess.SetCellText(i + 4, "B", string.Format("{0}", Dt.Rows[i]["wuliaomingcheng"]));
                                excelAccess.SetCellText(i + 4, "C", string.Format("{0}", Dt.Rows[i]["xinghaomingcheng"]));
                                excelAccess.SetCellText(i + 4, "D", string.Format("{0}", Dt.Rows[i]["shangqikucun"]));
                                excelAccess.SetCellText(i + 4, "E", string.Format("{0}", Dt.Rows[i]["benqijinchang"]));
                                excelAccess.SetCellText(i + 4, "F", string.Format("{0}", Dt.Rows[i]["benqikucun"]));
                                excelAccess.SetCellText(i + 4, "G", string.Format("{0}", Dt.Rows[i]["benqixiaohao"]));
                            }

                            excelAccess.MergeCells(4 + Dt.Rows.Count, "A", 4 + Dt.Rows.Count, "G", true);
                            excelAccess.SetCellText(4 + Dt.Rows.Count, "A", string.Format("制表人：{0}    制表时间：{1}", currentYongHu, DateTime.Now.ToString("yyyy-MM-dd HH:mm")));

                            Excel.Range r = excelAccess.GetRange(4, "A", Dt.Rows.Count + 3, "G");
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlLeft);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlRight);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlBottom);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideHorizontal);
                            excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideVertical);

                            string fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], fileNameResult);
                            if (jieshu.Contains(DateTime.Today.ToString("yyyy-MM-dd")))
                            {
                                fileName = string.Format("{0}report\\{1}.xls", m_PathList[keHuBianHao], Guid.NewGuid().ToString());
                            }
                            excelAccess.SaveAs(fileName, true);

                            string strHeader = "attachment;filename=" + string.Format("{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmss"));//把文件头输出，此文件头激活文件下载框  
                            currentC.Response.AppendHeader("Content-Disposition", strHeader);//http报头文件   
                            currentC.Response.ContentType = "application/ms-excel";
                            byte[] bs = File.ReadAllBytes(fileName);
                            currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                            currentC.Response.Close();
                        }
                        catch (Exception err)
                        {
                            KeaoUtils.LogTxt(string.Format("盘点表导出失败。{0}", err.Message));
                        }
                        finally
                        {
                            excelAccess.Close();
                        }


                    }
                    else
                    {
                        byte[] bs = Encoding.Default.GetBytes("<center>没有可导出的数据。</center>");
                        currentC.Response.OutputStream.Write(bs, 0, bs.Length);
                        currentC.Response.Close();
                        return;
                    }
                }

            }
            catch (Exception err)
            {
                KeaoUtils.LogTxt(string.Format("处理导出数据异常。{0}", err.Message));
            }

        }

        private void Form_Report_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                l.Abort();
            }
            catch (Exception err)
            {
                KeaoUtils.LogTxt(string.Format("关闭监听异常。{0}", err.Message));
            }
        }
    }
}
